package com.business_clean.viewmodel.request

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.business_clean.app.App
import com.business_clean.app.config.Constant
import com.business_clean.app.network.NetUrl
import com.business_clean.data.mode.contact.CompanyContactEntity
import com.business_clean.data.mode.group.GroupMangerEntity
import com.business_clean.data.mode.job.JobEntity
import com.business_clean.data.mode.members.IDCardMemberEntity
import com.business_clean.data.mode.members.MembersDetailEntity
import com.business_clean.data.mode.members.OCRBankInfoEntity
import com.business_clean.data.mode.members.OCRIDCardInfoEntity
import com.business_clean.data.mode.project.WorkRulesEntity
import com.business_clean.data.mode.roster.RosterEntity
import com.business_clean.ui.activity.custom.AddProjectActivity
import me.hgj.mvvmhelper.base.BaseViewModel
import me.hgj.mvvmhelper.ext.rxHttpRequest
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * 项目成员 增加， 编辑 离职
 */
class ProjectMembersViewModel : BaseViewModel() {


    //身份信息
    var idCardInfo = MutableLiveData<IDCardMemberEntity>()

    //身份编辑
    var updateIDCardStatus = MutableLiveData<IDCardMemberEntity>()

    //联系信息
    var updateContactsStatus = MutableLiveData<IDCardMemberEntity>()

    //银行卡编辑
    var updateBankStatus = MutableLiveData<IDCardMemberEntity>()

    //用功信息编辑
    var updateWorkInfoStatus = MutableLiveData<IDCardMemberEntity>()

    //薪酬标准
    var updateSalaryStatus = MutableLiveData<IDCardMemberEntity>()

    //头像/材料
    var updatePicStatus = MutableLiveData<IDCardMemberEntity>()

    //手写签名
    var updateSignHandStatus = MutableLiveData<IDCardMemberEntity>()

    //获取详情
    var membersDetailEntity = MutableLiveData<MembersDetailEntity>()

    //岗位列表
    var jobManger = MutableLiveData<JobEntity>()

    //ocr身份识别
    var ocrIdcardInfo = MutableLiveData<OCRIDCardInfoEntity>()

    var ocrBankInfo = MutableLiveData<OCRBankInfoEntity>()

    //人员的快速离职
    var depart = MutableLiveData<Any>()

    //快速离职获取到的详情
    var departDetailEntity = MutableLiveData<MembersDetailEntity>()

    //获取小组详情
    var groupMangerEntity = MutableLiveData<GroupMangerEntity>()

    //获取用工规则
    var workRulesEntity = MutableLiveData<WorkRulesEntity>()

    /**
     * 增加项目成员
     */
    fun requestIDCardMembers(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                idCardInfo.value = RxHttp.get(NetUrl.CREATE_MEMBERS)
                    .addAll(hashMap)
                    .toResponse<IDCardMemberEntity>().await()
            }
        }
    }

    /**
     * 编辑成员
     * edit 来区分是否是真的编辑 true 是已经存在的员工 false 是编辑的员工
     * 3 是档案想请，1、2都是草稿
     */
    fun requestUpdateMembers(hashMap: HashMap<String, String>, channel: String, type: Int) {
        rxHttpRequest {
            onRequest = {
                val updateStatusResult = RxHttp.get(if (type == 3) NetUrl.UPDATE_MEMBERS_UPDATE else NetUrl.UPDATE_MEMBERS)
                    .addAll(hashMap)
                    .toResponse<IDCardMemberEntity>().await()

                when (channel) {
                    AddProjectActivity.ID_CARD -> {
                        updateIDCardStatus.value = updateStatusResult
                    }

                    AddProjectActivity.ID_CONTACTS -> {
                        updateContactsStatus.value = updateStatusResult
                    }

                    AddProjectActivity.ID_BANK -> {
                        updateBankStatus.value = updateStatusResult
                    }

                    AddProjectActivity.ID_SALARY -> {
                        updateSalaryStatus.value = updateStatusResult
                    }

                    AddProjectActivity.ID_WORK_INFO -> {
                        updateWorkInfoStatus.value = updateStatusResult
                    }

                    AddProjectActivity.ID_PIC -> {
                        updatePicStatus.value = updateStatusResult
                    }

                    AddProjectActivity.ID_SIGN_HAND -> {
                        updateSignHandStatus.value = updateStatusResult
                    }

                    else -> {
                        updateIDCardStatus.value = updateStatusResult
                    }
                }
            }
        }
    }


    /**
     * 草稿档案详情
     */
    fun requestMembersDetail(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                membersDetailEntity.value = RxHttp.get(NetUrl.GET_MEMBERS_DETAILS)
                    .addAll(hashMap)
                    .toResponse<MembersDetailEntity>().await()
            }
        }
    }


    /**
     * 员工档案详情
     */
    fun requestStaffDetail(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                membersDetailEntity.value = RxHttp.get(NetUrl.GET_STAFF_DETAILS)
                    .addAll(hashMap)
                    .toResponse<MembersDetailEntity>().await()
            }
        }
    }


    /**
     * 获取岗位的列表
     */
    fun requestJobList(project_uuid: String) {
        rxHttpRequest {
            onRequest = {
                jobManger.value = RxHttp.get(NetUrl.JOB_LIST)
                    .add("project_uuid", project_uuid)
                    .toResponse<JobEntity>().await()
            }
        }
    }


    /**
     * 身份证识别
     */
    fun requestIDCardInfo(card_url: String) {
        rxHttpRequest {
            onRequest = {
                ocrIdcardInfo.value = RxHttp.get(NetUrl.OCR_ID_CARD_INFO)
                    .add("card_url", card_url)
                    .toResponse<OCRIDCardInfoEntity>().await()
            }
        }

    }

    /**
     * 银行卡识别
     */
    fun requestBankInfo(card_url: String) {
        rxHttpRequest {
            onRequest = {
                ocrBankInfo.value = RxHttp.get(NetUrl.OCR_BANK_INFO)
                    .add("card_url", card_url)
                    .toResponse<OCRBankInfoEntity>().await()
            }
        }

    }


    /**
     * 人员离职
     */
    fun requestMembersDepart(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                depart.value = RxHttp.get(NetUrl.DEPOART_MEMBERS)
                    .addAll(hashMap)
                    .toResponse<Any>().await()
            }
        }
    }


    /**
     * 获取审批的详情
     */
    fun requestDepartList(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                departDetailEntity.value = RxHttp.get(NetUrl.GET_DEPART_GET_ONE)
                    .addAll(hashMap)
                    .toResponse<MembersDetailEntity>().await()
            }
        }
    }


    /**
     * 获取全部
     */
    fun requestGroupAll(page: Int, project_uuid: String? = "") {
        rxHttpRequest {
            onRequest = {
                groupMangerEntity.value = RxHttp.get(NetUrl.GET_GROUP_ALL)
                    .add("page", page)
                    .add("size", Constant.PAGE_SIZE)
                    .add("project_uuid", if (!TextUtils.isEmpty(project_uuid)) project_uuid else App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<GroupMangerEntity>().await()
            }
        }
    }


    /**
     * 获取用工规则的配置
     */
    fun requestWorkRules(){
        rxHttpRequest {
            onRequest = {
                workRulesEntity.value = RxHttp.get(NetUrl.GET_WORK_POST_RULES)
                    .toResponse<WorkRulesEntity>().await()
            }
        }
    }

}